clc;clear;close all;
%{
神经网络训练主程序 with CEEMDAN
    written by GSX.
流程：
训练参数设置（在NARXConfig.txt文件中）——
数据读取——
设置神经网络的输入输出——
在narx_explanation.m中设置输入序列和输出序列的名称——
在result文件夹中查看结果。
%}

%% 数据读取
addpath('./NARX');
load mdm.mat
date=time;
t=t;    %日期序列数
makou_Q=q(:);
sanzao_z=z(:,1);
denglongshan_z=z(:,2);
zhuyin_z=z(:,3);
ganzhu_z=z(:,4);
makou_z=z(:,5);
makou_z_fixed=BPFF_fix_makou_z(date,t,makou_Q,makou_z);
% [imf_SZ,residual_SZ] = emd(sanzao_z,'MaxNumExtrema',1,'SiftRelativeTolerance',0.1,'MaxNumIMF',20);
% [imf_DLS,residual_DLS] = emd(denglongshan_z,'MaxNumExtrema',1,'SiftRelativeTolerance',0.1,'MaxNumIMF',20);
% [imf_ZY,residual_ZY] = emd(zhuyin_z,'MaxNumExtrema',1,'SiftRelativeTolerance',0.1,'MaxNumIMF',20);
% [imf_GZ,residual_GZ] = emd(ganzhu_z,'MaxNumExtrema',1,'SiftRelativeTolerance',0.1,'MaxNumIMF',20);

%% 读取导入的训练设置
[outputSetting,param]=configInput('NARXConfig.txt');  %导入训练设置
% 生成图片对应的训练参数文档说明
NARX_explanation(outputSetting,param);

for station=1:3
    for i=1:param.trainCount
        %     for k=1:11   %对各emd分量建模,取前十个emd分量，第十一个为residual
        if outputSetting.isDate==1
            param=param.date_to_index(t);  %将日期转化为索引
        end
        trainBegin=param.trainBegin{i};       %用于训练的数据的开始索引
        trainEnd=param.trainEnd{i};      %用于训练的数据的结束索引
        predictBegin=param.predictBegin{i};       %用于预测的数据的开始索引
        predictEnd=param.predictEnd{i};      %用于预测的数据的结束索引
        if trainBegin==0
            continuePredict=true;   %是否使用上一次的训练结果预测这一次输入
        else
            continuePredict=false;
        end
        % 神经网络部分设置
        inputDelays = param.inputDelays{i};    %输入延迟
        feedbackDelays = param.feedbackDelays{i};    %输出延迟
        hiddenLayerSize = param.hiddenLayerSize{i};    %隐藏层神经元数
        
        %% 设置神经网络的输入输出
        %         if continuePredict==false
        %             makou_Q_train=q(trainBegin:trainEnd);
        %             sanzao_z_train=imf_SZ(trainBegin:trainEnd,k);
        %             denglongshan_z_train=imf_DLS(trainBegin:trainEnd,k);
        %             zhuyin_z_train=imf_ZY(trainBegin:trainEnd,k);
        %             ganzhu_z_train=imf_GZ(trainBegin:trainEnd,k);
        %             if k==11 %残余
        %             sanzao_z_train=residual_SZ(trainBegin:trainEnd);
        %             denglongshan_z_train=residual_DLS(trainBegin:trainEnd);
        %             zhuyin_z_train=residual_ZY(trainBegin:trainEnd);
        %             ganzhu_z_train=residual_GZ(trainBegin:trainEnd);
        %             end
        %         end
        %         makou_Q_predict=q(predictBegin:predictEnd);
        %         sanzao_z_predict=imf_SZ(predictBegin:predictEnd,k);
        %         denglongshan_z_predict=imf_DLS(predictBegin:predictEnd,k);
        %         zhuyin_z_predict=imf_ZY(predictBegin:predictEnd,k);
        %         ganzhu_z_predict=imf_GZ(predictBegin:predictEnd,k);
        %         if k==11
        %             sanzao_z_predict=residual_SZ(predictBegin:predictEnd);
        %             denglongshan_z_predict=residual_DLS(predictBegin:predictEnd);
        %             zhuyin_z_predict=residual_ZY(predictBegin:predictEnd);
        %             ganzhu_z_predict=residual_GZ(predictBegin:predictEnd);
        %         end
        if continuePredict==false
            makou_Q_train=q(trainBegin:trainEnd);
            sanzao_z_train=z(trainBegin:trainEnd,1);
            denglongshan_z_train=z(trainBegin:trainEnd,2);
            zhuyin_z_train=z(trainBegin:trainEnd,3);
            ganzhu_z_train=z(trainBegin:trainEnd,4);
            makou_z_train=makou_z_fixed(trainBegin:trainEnd);
        end
        makou_Q_predict=q(predictBegin:predictEnd);
        sanzao_z_predict=z(predictBegin:predictEnd,1);
        denglongshan_z_predict=z(predictBegin:predictEnd,2);
        zhuyin_z_predict=z(predictBegin:predictEnd,3);
        ganzhu_z_predict=z(predictBegin:predictEnd,4);
        makou_z_predict=makou_z_fixed(predictBegin:predictEnd);
        
        
        % 输入和目标序列设置
        inputTrain=[makou_Q_train sanzao_z_train makou_z_train];   %输入的训练用时间序列
        %targetTrain=[ganzhu_z_train zhuyin_z_train denglongshan_z_train];   %目标训练时间序列
        inputPredict=[makou_Q_predict sanzao_z_predict makou_z_predict];   %输入的预测时间序列
        %targetPredict=[ganzhu_z_predict zhuyin_z_predict denglongshan_z_predict];   %目标预测时间序列
        
        %% 以上述参数训练NARX神经网络,绘制结果并保存
        switch station
            case 1
                output_GZ=NARX_predict_gsx(outputSetting,i, ...
                    inputTrain,ganzhu_z_train,inputPredict,ganzhu_z_predict, ...
                    inputDelays,feedbackDelays,hiddenLayerSize,continuePredict,'GZ');
                NARX_plot_CEEMDAN(outputSetting,inputDelays,output_GZ,ganzhu_z(predictBegin:predictEnd),strcat('GZ',num2str(i)));
            case 2
                output_ZY=NARX_predict_gsx(outputSetting,i, ...
                    inputTrain,zhuyin_z_train,inputPredict,zhuyin_z_predict, ...
                    inputDelays,feedbackDelays,hiddenLayerSize,continuePredict,'ZY');
                NARX_plot_CEEMDAN(outputSetting,inputDelays,output_ZY,zhuyin_z(predictBegin:predictEnd),strcat('ZY',num2str(i)));
            case 3
                output_DLS=NARX_predict_gsx(outputSetting,i, ...
                    inputTrain,denglongshan_z_train,inputPredict,denglongshan_z_predict, ...
                    inputDelays,feedbackDelays,hiddenLayerSize,continuePredict,'DLS');
                NARX_plot_CEEMDAN(outputSetting,inputDelays,output_DLS,denglongshan_z(predictBegin:predictEnd),strcat('DLS',num2str(i)));
        end

    end
end
